/****************************************************
          File Name:job_v3.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 12 Aug 2014 04:19:32 PM CST
*****************************************************/

#include <stdio.h>
#include "head.h"
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  job j_timer;

  // chdir("/home/abby/");

  if (argc < 2 || strcmp (argv[1], "--help") == 0)
    {
      printf ("%s -[acdfs] (para) ...\n", argv[0]);
      printf ("\t:-a to delete all timer\n");
      printf
	("\t:-c time command to create a timer; example:%s -c 18:00:00 \"du -h\"\n",
	 argv[0]);
      printf ("\t:-d to delete the oldest timer\n");
      printf
	("\t:-f to use a specific file in the format like: Tue Aug 12 18:02:27 2014\n");
      printf ("\t:-s to show all exist timer in file\n");
      exit (EXIT_FAILURE);
    }
  else if (strcmp (argv[1], "-d") == 0 || strcmp (argv[1], "-a") == 0)
    deltimer (argv[1]);
  else if (strcmp (argv[1], "-s") == 0)
    showtimer (j_timer, 'r');
  else
    {
      switch (fork ())
	{
	case -1:
	  eRR ("fork()");
	case 0:
	  j_timer = gettime_cmd (argv[1], argv[2], argv[3]);
	  j_timer.pid = getpid ();
	  setsid ();
	  settimer (j_timer);
	  printf ("timer expired in %lu s\n", j_timer.relative_time);
	  pause ();
	default:
	  break;
	}
    }
  exit (EXIT_SUCCESS);
}
